<?php
class M_sections extends MY_Model {
	
	private $__table = 'sections';
	
	public function get_section_by_grade_level_array($level_id)
	{
		$query = "SELECT id,section_name 
				  FROM sections 
				  WHERE grade_level_id = ? AND max_load != current_load AND max_load != 0";
			$q = $this->db->query($query,array($level_id));
			
			if($q->num_rows() >= 1)
			{
				$sections = $q->result();
				$opt[''] ='--- Select Section ---';
				foreach($sections as $section){
					$opt[$section->id] = $section->section_name;
				}
			}else{
				$opt[''] = 'Sections is not available';
			}
			return  $opt;
	}

	public function get_section_data($enrollment_id)
	{
		$sql = 'SELECT s.id as sec_id,
							s.section_name,
							e.emp_id,
							e.id as user_id,
							concat_ws("",e.last_name," , ",e.first_name) as fullname
				  FROM sections s
				  LEFT JOIN employees e ON e.emp_id = s.adviser_id
				  LEFT JOIN enrollments en ON en.e_section_id = s.id
				  WHERE en.e_id = ?';

		$q = $this->db->query($sql,[$enrollment_id]);

		if($q->num_rows()>=1)
		{
			$res = $q->row();
			$section['data'] = $res;
			$section['male'] = $this->_get_student_by_section($res->sec_id,'male');
			$section['female'] = $this->_get_student_by_section($res->sec_id,'male');

			return $section;
		}else{
			return FALSE;
		}
	}

	public function verify_section($section_id)
	{
		$sql = 'SELECT id FROM sections WHERE id = ? AND year_id = ?';
		$res = $this->db->query($sql,[$section_id,$this->c_sy->id]);

		return $res->num_rows() >= 1 ? TRUE : FALSE;
	}

	public function _get_student_by_section($section_id,$gender)
	{
		$count = 'SELECT count(e.e_id) as total
				  FROM enrollments e
				  LEFT JOIN profiles p ON p.profile_id = e.e_profile_id
				  WHERE e.e_section_id = ?
				  AND p.gender = ?';

		$qcount = $this->db->query($count,[$section_id,$gender]);
		return $qcount->num_rows() >=1 ? $qcount->row()->total : 0;
	}

	public function get_section_for_dd($e_id = FALSE)
	{
		if($e_id == FALSE)
		{
			$CI =& get_instance();
			$user = $CI->session->userdata('userid');
			$CI->load->model('M_Parent');
			$userstudents = $CI->M_Parent->get_user_students($user);

			if($userstudents)
			{
				foreach($userstudents as $k => $v)
				{
					$e_ids[] = $v->e_id;
				}

				if(!empty($e_ids))
				{
					
					$eid = '"'.implode('","',$e_ids).'"';
					
					$sql = 'SELECT distinct(s.section_name) as sec,id
					  FROM enrollments e
					  LEFT JOIN sections s ON e.e_section_id = s.id
					  WHERE e.e_id IN ('.$eid.')';


					$res = $this->db->query($sql);
					if($res->num_rows() >=1 )
					{
						$level[''] = '--Select Section --';
						foreach($res->result() as $k => $v)
						{
							if($v->id !== NULL && $v->sec !== NULL){
								$level[$v->id] = $v->sec;
							}else{
								
							}
						}

						return $level;

					}else{
						return [''=>'No Section Found'];
					}
				}else{
					return [''=>'No Section Found'];
				}
			}else{
				return [''=>'No Section Found'];
			}
		}else{

		}
	}
	
	public function get_section_for_me($e_id = FALSE)
	{
		if($e_id == FALSE)
		{
			$CI =& get_instance();
			$user = $CI->session->userdata('id_e');
					
			$sql = 'SELECT distinct(s.section_name) as sec,id
			  FROM enrollments e
			  LEFT JOIN sections s ON e.e_section_id = s.id
			  WHERE e.e_id = '.$user.' ';

			$res = $this->db->query($sql);

			if($res->num_rows() >=1 )
			{
				return $res->row();
			}else{
				return FALSE;
			}
				
		}else{
			return FALSE;
		}
	}
	
	public function get_section_by_grade_level($level_id)
	{
		$detect_if_block = $this->db->select('id,block_name,block_desc')
									->where('block_level',$level_id)
									->get('block_section');
									
									
		if($detect_if_block->num_rows() >=1)
		{
			return $detect_if_block->num_rows() >= 1 ? $detect_if_block->result() : FALSE;
		}else{
			return 'NOBLOCK';
		}

		
	}
}
?>